import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../components/Login.vue'
import Reg from '../components/RegUser.vue'
Vue.use(VueRouter)

  const routes = [
    // 路由重定向
    {
      path:'/',
      redirect:'/login'
    },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path:'/reg',
    name:'Reg',
    component:Reg
  }
]

const router = new VueRouter({
  routes
})

// 设置全局路由导航守卫判断登录状态来达到未登录跳转
//挂载
router.beforeEach((toUrl,fromUrl,next)=>{
  console.log(toUrl)
  //to为将要去的路由
  //from表示来源
  //next表示放行函数
  if(toUrl.path=="/login") return next();
  if(toUrl.path=='/home'){
    var token=window.sessionStorage.getItem('token');
    if(token){
      return next()
    }else{
      return next('/login')
    }
  }
})



export default router
